package com.itheima.Tree.BFS;
import com.itheima.Tree.TreeNode;
import org.w3c.dom.Node;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.IntBinaryOperator;

/**
 * @Author: Ethan Yankang
 * @Program: code-random-recordings
 * @Date: 2024-08-02 20:01
 **/


public class LevelOrder {
   public List<List<Integer>> resList= new LinkedList<List<Integer>>();
   public List<List<Integer>> levelOrder(TreeNode root) {
       BFSByQueue(root);
       return resList;
   }

    private void BFSByQueue(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()){
            LinkedList<Integer> item=new LinkedList<>();
            int size=queue.size();

            while (size-->0){
                TreeNode tempNode=queue.remove();
                item.add(tempNode.val);
                if (tempNode.left!=null){
                    queue.add(tempNode.left);
                }
                if (tempNode.right!=null){
                    queue.add(tempNode.right);
                }
            }
            resList.add(item);
        }
    }
}
